package com.boss.ccs.gis;

import com.boss.Messages;
import com.boss.core.BossConstants;
import com.boss.core.common.Artifact;
import com.boss.core.form.BossForm;
import com.boss.core.form.BossFormHelper;
import com.boss.core.util.BossUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * GIS PAM Form
 * @author Momin Noor Khan
 */
public class BossGISPAMForm extends BossForm {
    private String artifacts = "";
    private boolean isArtifactSection = false;
    private boolean isTargetSection;
    private String targetProfile;
    private String gisCustomers;

    //Getters and Setters
    public boolean isArtifactSection() {
        return isArtifactSection;
    }

    public void setArtifactSection(boolean artifactSection) {
        isArtifactSection = artifactSection;
    }

    public String getArtifacts() {
        return artifacts;
    }

    public void setArtifacts(String artifacts) {
        this.artifacts = artifacts;
    }

    public boolean isTargetSection() {
        return isTargetSection;
    }

    public void setTargetSection(boolean targetSection) {
        isTargetSection = targetSection;
    }

    public String getTargetProfile() {
        return targetProfile;
    }

    public void setTargetProfile(String targetProfile) {
        this.targetProfile = targetProfile;
    }

    public String getGisCustomers() {
        return gisCustomers;
    }

    public void setGisCustomers(String gisCustomers) {
        this.gisCustomers = gisCustomers;
    }

    public BossGISPAMForm(StaplerRequest req, JSONObject jsonForm, String project) {
        super(req, jsonForm, project);
        setAvailableJob(BossUtil.findAvailableJob(BossConstants.GIS_KEY));
        isArtifactSection = BossUtil.contains(getJsonTaskSection(), BossConstants.KEY_ARTIFACT);
        artifacts = getSelectedArtifacts(isArtifactSection, getRequest());
        isTargetSection = BossUtil.contains(getJsonTaskSection(), BossConstants.KEY_TARGET);
        targetProfile = BossFormHelper.getGisTargetProfile(isTargetSection, req);
        gisCustomers = BossFormHelper.getSelectedGisCustomers(getTask(), getJsonTaskSection());
    }

    public String getSelectedArtifacts(boolean hasArtifactSection, StaplerRequest req) {
        String artifacts = "";
        if (hasArtifactSection) {
            try {
                artifacts = getSelectedArtifacts(req);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return artifacts;
    }

    /**
     * Gets the artifacts selected by the user on the form from the repeatable 'Artifact' section.
     * @param req
     * @throws java.io.IOException
     * @throws javax.servlet.ServletException
     */
    public String getSelectedArtifacts(StaplerRequest req) throws IOException, ServletException {
        String retArtifactInfo = "";
        List<String> list = new ArrayList<String>();
        List<Artifact> artifacts = new ArrayList<Artifact>();
        JSONObject jsonForm = req.getSubmittedForm();
        artifacts.addAll(req.bindJSONToList(Artifact.class, jsonForm.getJSONObject("Task").get("artifact")));

        for (Artifact artifact : artifacts) {
            if (!BossFormHelper.isBlankOrDummy(artifact)) {
                //format is modulename@versionnumber;
                String item = artifact.getModuleName() + "@" + artifact.getVersionNumber() + ";";
                //add to list if it is not a duplicate
                if (!list.contains(item)) {
                    list.add(item);
                }
            }
        }

        if (list.size() != 0) {
            for (String artifact : list) {
                retArtifactInfo = retArtifactInfo + artifact;
            }
            //remove the trailing semi-colon (if any)
            retArtifactInfo = StringUtils.removeEnd(retArtifactInfo, ";");
        }
        return retArtifactInfo;
    }

    @Override
    public void prepareUrlParamMap() {
        super.prepareUrlParamMap();
        Map<String, String> map = getUrlMap();
        map.put(BossConstants.URL_PARAM_PROJECT, getProject());
        map.put(BossConstants.URL_PARAM_ARTIFACTS, artifacts);
        map.put(BossConstants.URL_PARAM_TARGET_PROFILE, targetProfile);
        map.put(BossConstants.URL_PARAM_GIS_CUSTOMERS, gisCustomers);
        setUrlMap(map);
    }

    @Override
    public boolean isValid() {
        boolean valid = true;
        if (!checkMandatoryFields() |
                !checkArtifacts() |
                !checkTargetProfile()
                ) {
            valid = false;
        } else if (!BossFormHelper.checkAvailableJob(getRequest(), getAvailableJob())) {
            valid = false;
        }
        return valid;
    }

    //Validation methods
    public boolean checkMandatoryFields() {
        boolean valid = true;
        StaplerRequest req = getRequest();
        String task = getTask();
        if (!BossFormHelper.checkTask(req, task)) {
            valid = false;
        }
        return valid;
    }

    public boolean checkArtifacts() {
        boolean valid = true;
        StaplerRequest req = getRequest();
        String task = getTask();
        String artifacts = getArtifacts();
        if (task.equals(BossConstants.TASK_COPY_BUILD)) {
            if (BossUtil.isBlankOrDummy(artifacts)) {
                BossFormHelper.setValidationErrorMsg(req, Messages.Boss_MandatoryArtifact(task));
                valid = false;
            }
        }
        return valid;
    }

    public boolean checkTargetProfile() {
        boolean valid = true;
        StaplerRequest req = getRequest();
        String task = getTask();
        String targetProfile = getTargetProfile();
        if (task.equals(BossConstants.TASK_COPY_BUILD)) {
            if (BossUtil.isBlankOrDummy(targetProfile)) {
                BossFormHelper.setValidationErrorMsg(req, Messages.Boss_MandatoryTarget(task));
                valid = false;
            }
        }
        return valid;
    }
}
